<template>
  <el-dialog :title="title" :visible.sync="dialogVisible" :before-close="handleCancel" center width="800px">
    <el-form ref="editFormRef" :model="editForm" :rules="rules" size="small">
      <el-form-item label="年级名称" :label-width="labelWidth" prop="name" class="line">
        <el-input v-model="editForm.name" auto-complete="off"  placeholder="请输入年级名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="学段" :label-width="labelWidth" prop="period" class="line">
          <el-select v-model="editForm.period"
            clearable
            placeholder="请选择课学段"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in periodList"
              :key="item.id"
              :label="item.period"
              :value="item.period"
            ></el-option>
          </el-select>
      </el-form-item>
      <el-form-item label="入学年份" :label-width="labelWidth" prop="year" class="line">
        <el-date-picker
        style="width: 100%"
        v-model="editForm.year"
        type="year"
        placeholder="选择日期"
        size="small"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="年级组长" :label-width="labelWidth" prop="leaderNo" class="line">
          <el-select  v-model="editForm.leaderNo"
            style="width:100%"
            clearable
            placeholder="请选择年级组长"
            filterable
          >
            <el-option
              v-for="item in teacherList"
              :key="item.number"
              :label="item.name"
              :value="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="状态" :label-width="labelWidth" prop="status" class="line">
          <el-select v-model="editForm.status"
            clearable
            placeholder="请选择状态"
            filterable
            style="width: 100%"
          >
            <el-option
              v-for="item in statusList"
              :key="item.id"
              :label="item.status"
              :value="item.id"
            ></el-option>
          </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel" size="small">取 消</el-button>
      <el-button type="primary" @click="hanldeSave('editFormRef')" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import Vue from 'vue'
import { teacherList } from '../js/gradeInfo'
import { addGradeInfo, updateGradeInfo } from '@/views/children/basic/js/gradeInfo'
import pick from 'lodash.pick'
import { USER_INFO } from '@/views/children/workOrder/js/constants'
import moment from 'moment'
export default {
  name: 'gradeInfoModal',
  data () {
    return {
      tenantNo: Vue.ls.get(USER_INFO).tenantNo,
      title: '新增年级',
      isAdd: false,
      disabled: false,
      dialogVisible: false,
      labelWidth: '120px',
      editForm: {
        name: '',
        period: '',
        year: '',
        leaderNo: '',
        status: ''
      },
      model: {},
      rules: {
        name: [{ required: true, validator: this.validateName }],
        period: [{ required: true, validator: this.validatePeriod }],
        year: [{ required: true, validator: this.validateYear }],
        leaderNo: [{ required: true, validator: this.validateLeaderNo }],
        status: [{ required: true, validator: this.validateStatus }]
      },
      periodList: [{ id: 1, period: '小学' }, { id: 2, period: '初中' }, { id: 3, period: '高中' }],
      statusList: [{ id: 0, status: '作废' }, { id: 1, status: '正常' }, { id: 2, status: '毕业' }],
      teacherList: []
    }
  },
  props: {},
  computed: {},
  filters: {},
  mounted () {
    this.getTeacherList()
  },
  methods: {
    add () {
      this.edit({})
    },
    edit (record) {
      this.dialogVisible = true
      this.model = Object.assign({}, record)
      console.log(record)
      this.$nextTick(() => {
        if (record.id) {
          this.title = '修改年级'
          this.editForm = pick(this.model, 'name', 'period', 'year', 'leaderNo', 'status')
        } else {
          this.title = '新增年级'
          // this.editForm.insideNotice = 0
          // this.editForm.mobileNotice = 0
        }
      })
    },
    // 提交表单
    hanldeSave (editFormRef) {
      this.$refs[editFormRef].validate(async valid => {
        if (valid) {
          let res, msg
          const values = {
            name: this.editForm.name,
            period: this.editForm.period,
            year: moment(this.editForm.year).format('YYYY').toString(),
            leaderNo: this.editForm.leaderNo,
            status: this.editForm.status
          }
          if (!this.model.id) {
            console.log(values)
            res = await addGradeInfo(Object.assign({}, values))
            msg = '保存成功！'
          } else {
            console.log(values)
            res = await updateGradeInfo(Object.assign({}, values, { id: this.model.id }))
            msg = '修改成功！'
          }
          if (res.code === 0) {
            this.$message.success(msg)
          } else {
            this.$message.warning(res.msg)
          }
          this.$emit('ok')
          this.handleCancel()
        }
      })
    },
    async  getTeacherList () {
      let res = await teacherList({})
      console.log(res)
      this.teacherList = res.data
    },
    handleCancel () {
      this.dialogVisible = false
      this.model = {}
      this.$refs.editFormRef.resetFields()
    },
    strlen (str) {
      var len = 0
      for (var i = 0; i < str.length; i++) {
        var c = str.charCodeAt(i)
        // 单字节加1
        if ((c >= 0x0001 && c <= 0x007e) || (c >= 0xff60 && c <= 0xff9f)) {
          len++
        } else {
          len += 2
        }
      }
      return len
    },
    validateName (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入年级名称'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('年级名称不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入年级名称'))
      }
    },
    validatePeriod (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入学段'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('学段不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入学段'))
      }
    },
    validateYear (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入入学年份'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('入学年份不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入入学年份'))
      }
    },
    validateLeaderNo (rule, value, callback) {
      if (value) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入年级组长'))
        } else if (this.strlen(value) > 30) {
          callback(new Error('年级组长不能超过30个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入年级组长'))
      }
    },
    validateStatus (rule, value, callback) {
      if (value || value === 0) {
        if (value.toString().trim() === '') {
          callback(new Error('请输入状态'))
        } else if (this.strlen(value) > 80) {
          callback(new Error('状态不能超过80个字符'))
        } else {
          callback()
        }
      } else {
        callback(new Error('请输入状态'))
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}

/deep/ .el-rate__item {
  padding-top: 5px;
  .el-rate__icon {
    font-size: 22px;
  }
}
/deep/ .img-over {
  margin-right: 5px;
  width: 60px;
  height: 45px;
  cursor: pointer;
}
/deep/ .viewer-container {
  z-index: 3000 !important;
}
.line{
  display: inline-block;
  width: 350px;
}
</style>
